/***********************************************************
  > File Name: hiredisTest.cpp
  > Author: Mou
  > Mail: 2429826959@qq.com
  > Created Time: Fri 16 Sep 2022 07:44:16 PM CST
  > Modified Time:Fri 16 Sep 2022 07:44:16 PM CST
 *******************************************************/
#include <hiredis/hiredis.h>
#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::cerr;

int main()
{
    redisContext *pConnect = redisConnect("127.0.0.1", 6379);
    if(pConnect == nullptr || pConnect->err)
    {
        if(pConnect)
        {
            cerr << pConnect->errstr;
            redisFree(pConnect);
            return -1;
        }
        else 
        {
            cout << "can not allocate redis context" << "\n";
            return -1;
        }
    }

    redisReply *pReply = (redisReply *) redisCommand(pConnect, "set %s %d", "boldness", 30);

    if(pReply == nullptr)
    {
        cout << "command error" << "\n";
        redisFree(pConnect);
        return -1;
    }

    if(pReply->type == REDIS_REPLY_NIL)
    {
        cout << "command error" << "\n";
    }

    pReply = (redisReply *) redisCommand(pConnect, "get %s", "boldness");

    if(pReply->type == REDIS_REPLY_NIL)
    {
        cout << "get nil" << "\n";
    }
    if(pReply->type == REDIS_REPLY_STRING)
    {
        cout << "get value" << pReply->str << "\n";
    }

    freeReplyObject(pReply);
    redisFree(pConnect);

    return 0;
}

